package org.jeecg.modules.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.controller.dao.DisPlayMerchantInfoDao;
import org.jeecg.modules.hc.entity.HcCompanyFoodDisplay;
import org.jeecg.modules.hc.entity.HcCompanyFoodList;
import org.jeecg.modules.hc.service.IHcCompanyFoodDisplayService;
import org.jeecg.modules.hc.service.IHcCompanyFoodListService;
import org.jeecg.modules.hc.service.IHcCompanyInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 企业订餐用户菜品展示
 * @Author: jeecg-boot
 * @Date:   2023-07-11
 * @Version: V1.0
 */
@Api(tags="企业订餐用户菜品展示")
@RestController
@RequestMapping("/hc/hcCompanyFoodDisplay")
@Slf4j
public class HcCompanyFoodDisplayController extends JeecgController<HcCompanyFoodDisplay, IHcCompanyFoodDisplayService> {
	@Autowired
	private IHcCompanyFoodDisplayService hcCompanyFoodDisplayService;
	 @Autowired
	 private IHcCompanyInfoService hcCompanyInfoService;
	@Autowired
	private IHcCompanyFoodListService hcCompanyFoodListService;

	/**
	 * 添加展示菜品
	 * @return
	 */
	@ApiOperation(value="添加展示菜品", notes="添加展示菜品")
	@PostMapping(value = "/addDisPlayProd")
	public Result<?> addDisPlayProd(@RequestBody HcCompanyFoodList HcCompanyFoodList) {
		hcCompanyFoodDisplayService.addDisPlayProd(HcCompanyFoodList);
		return Result.OK("新增成功");
	}

	 /**
	  * 根据公司查询对应展示的菜品
	  * @return
	  */
	 @ApiOperation(value="根据公司查询对应展示的菜品", notes="根据公司查询对应展示的菜品")
	 @GetMapping(value = "/queryByCompanyId")
	 public Result<?> queryByCompanyId(@RequestParam(name="companyId") String companyId,
									   @RequestParam(name="detailId") String detailId) {
		 return Result.OK(hcCompanyFoodDisplayService.queryByCompanyId(companyId,detailId));
	 }

	/**
	 * 根据公司查询对应展示的菜品
	 * @return
	 */
	@ApiOperation(value="根据公司查询对应展示的菜品（用户）", notes="根据公司查询对应展示的菜品")
	@GetMapping(value = "/queryByCompanyIdNew")
	public Result<?> queryByCompanyIdNew(@RequestParam(name="companyId") String companyId) {
		return Result.OK(hcCompanyFoodDisplayService.queryByCompanyIdNew(companyId));
	}

	/**
	 * 门店设置
	 */
//	@ApiOperation(value="门店设置", notes="门店设置")
	@PostMapping(value = "/setMerchantInfo")
	public Result<?> setMerchantInfo(@RequestBody DisPlayMerchantInfoDao disPlayMerchantInfoDao) {
		hcCompanyFoodDisplayService.setMerchantInfo(disPlayMerchantInfoDao);
		return Result.OK("设置成功");
	}


	/**
	 * 分页列表查询
	 *
	 * @param hcCompanyFoodDisplay
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "企业订餐用户菜品展示-分页列表查询")
	@ApiOperation(value="企业订餐用户菜品展示-分页列表查询", notes="企业订餐用户菜品展示-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HcCompanyFoodDisplay>> queryPageList(HcCompanyFoodDisplay hcCompanyFoodDisplay,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HcCompanyFoodDisplay> queryWrapper = QueryGenerator.initQueryWrapper(hcCompanyFoodDisplay, req.getParameterMap());
		Page<HcCompanyFoodDisplay> page = new Page<HcCompanyFoodDisplay>(pageNo, pageSize);
		IPage<HcCompanyFoodDisplay> pageList = hcCompanyFoodDisplayService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hcCompanyFoodDisplay
	 * @return
	 */
	@AutoLog(value = "企业订餐用户菜品展示-添加")
	@ApiOperation(value="企业订餐用户菜品展示-添加", notes="企业订餐用户菜品展示-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HcCompanyFoodDisplay hcCompanyFoodDisplay) {
		String foodsListIds = hcCompanyFoodDisplay.getFoodsListIds();
		List<String> strings = Arrays.asList(foodsListIds.split(","));
		strings.forEach(c-> {
			HcCompanyFoodDisplay companyFoodDisplay = new HcCompanyFoodDisplay();
			BeanUtils.copyProperties(hcCompanyFoodDisplay,companyFoodDisplay);
			companyFoodDisplay.setFoodsListId(c);
			hcCompanyFoodDisplayService.save(companyFoodDisplay);
		});
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hcCompanyFoodDisplay
	 * @return
	 */
	@AutoLog(value = "企业订餐用户菜品展示-编辑")
	@ApiOperation(value="企业订餐用户菜品展示-编辑", notes="企业订餐用户菜品展示-编辑")
	//@RequiresPermissions("org.jeecg.modules:hc_company_food_display:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HcCompanyFoodDisplay hcCompanyFoodDisplay) {
		hcCompanyFoodDisplayService.updateById(hcCompanyFoodDisplay);
		return Result.OK("编辑成功!");
	}


	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "删除企业订餐用户菜品展示")
	@ApiOperation(value="删除企业订餐用户菜品展示", notes="删除企业订餐用户菜品展示")
	//@RequiresPermissions("org.jeecg.modules:hc_company_food_display:delete")
	@DeleteMapping(value = "/deleteNew")
	public Result<String> deleteNew(@RequestParam(name="id",required=true) String id,@RequestParam(name="detailsId",required=true) String detailsId) {
		LambdaQueryWrapper<HcCompanyFoodDisplay> hcCompanyFoodDisplayLambdaQueryWrapper = new LambdaQueryWrapper<>();
		hcCompanyFoodDisplayLambdaQueryWrapper.eq(HcCompanyFoodDisplay::getFoodsListId,id).eq(HcCompanyFoodDisplay::getCompanyDetailId,detailsId);
		hcCompanyFoodDisplayService.remove(hcCompanyFoodDisplayLambdaQueryWrapper);
		return Result.OK("删除成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
//	@AutoLog(value = "企业订餐用户菜品展示-通过id删除")
//	@ApiOperation(value="企业订餐用户菜品展示-通过id删除", notes="企业订餐用户菜品展示-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_food_display:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		hcCompanyFoodDisplayService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
//	@AutoLog(value = "企业订餐用户菜品展示-批量删除")
//	@ApiOperation(value="企业订餐用户菜品展示-批量删除", notes="企业订餐用户菜品展示-批量删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_food_display:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hcCompanyFoodDisplayService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "企业订餐用户菜品展示-通过id查询")
	@ApiOperation(value="企业订餐用户菜品展示-通过id查询", notes="企业订餐用户菜品展示-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HcCompanyFoodDisplay> queryById(@RequestParam(name="id",required=true) String id) {
		HcCompanyFoodDisplay hcCompanyFoodDisplay = hcCompanyFoodDisplayService.getById(id);
		if(hcCompanyFoodDisplay==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hcCompanyFoodDisplay);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hcCompanyFoodDisplay
    */
    //@RequiresPermissions("org.jeecg.modules:hc_company_food_display:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcCompanyFoodDisplay hcCompanyFoodDisplay) {
        return super.exportXls(request, hcCompanyFoodDisplay, HcCompanyFoodDisplay.class, "企业订餐用户菜品展示");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("hc_company_food_display:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcCompanyFoodDisplay.class);
    }

}
